import numpy as np
from collections import Counter

def simple_knn_example():
    """最简单的KNN示例"""
    
    # 训练数据：[[身高, 体重], 喜欢篮球?]
    training_data = [
        [[180, 75], 1],  # 喜欢篮球
        [[175, 70], 1],  # 喜欢篮球
        [[165, 55], 0],  # 不喜欢篮球
        [[170, 60], 0],  # 不喜欢篮球
    ]
    
    # 新同学：[178, 72]
    new_student = [178, 72]
    
    # 计算距离并找出最近邻居
    distances = []
    for features, label in training_data:
        distance = np.sqrt((features[0]-new_student[0])**2 + 
                         (features[1]-new_student[1])**2)
        distances.append((distance, label))
    
    # 找出最近的3个邻居
    distances.sort(key=lambda x: x[0])
    nearest_neighbors = distances[:3]
    
    # 投票决定
    votes = [label for _, label in nearest_neighbors]
    result = Counter(votes).most_common(1)
    
    print(f"预测结果: {'喜欢篮球' if result == 1 else '不喜欢篮球'}")
    return result

# 运行示例
simple_knn_example()